package cn.wolfcode.web.controller;


import cn.wolfcode.domain.ConsumptionItem;
import cn.wolfcode.service.IConsumptionItemService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

@Controller
@RequestMapping("/consumptionItem")
public class ConsumptionItemController {

    @Autowired
    private IConsumptionItemService consumptionItemService;


    @RequiredPermission(name="计算单明细页面",expression="consumptionItem:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<ConsumptionItem> pageInfo = consumptionItemService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "consumptionItem/list";
    }


    @RequiredPermission(name="计算单明细删除",expression="consumptionItem:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           consumptionItemService.delete(id);
        }
        return "redirect:/consumptionItem/list";
    }


    @RequiredPermission(name="计算单明细新增/编辑",expression="consumptionItem:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(ConsumptionItem consumptionItem) {
        if (consumptionItem.getId() == null) {
            consumptionItemService.save(consumptionItem);
        } else {
            consumptionItemService.update(consumptionItem);
        }
        return "redirect:/consumptionItem/list";
    }


    @RequiredPermission(name="计算单明细新增/编辑",expression="consumptionItem:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("consumptionItem",consumptionItemService.get(id));
        }
        return "consumptionItem/input";
    }
}
